package cn.learn.controller.cargo;

import cn.learn.base.BaseController;
import cn.learn.service.cargo.ContractService;
import cn.learn.utils.commons.DownloadUtil;
import cn.learn.vo.ContractProductVo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletContext;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/cargo/contract")
public class OutProductController extends BaseController {

    @Reference
    private ContractService contractService;

    @RequestMapping(value = "/print",name = "到出货表页面")
    public String print(){

        return "cargo/print/contract-print";
    }


    @RequestMapping(value = "/printExcel",name = "导出出货表")
    public void printExcel2(String inputDate) throws IOException {

        //1 查询出货表需要的数据 List<ContractProductVo>
        List<ContractProductVo> list = contractService.findOutData(companyId,inputDate);
        System.out.println(list.size()); //163
        //2 将数据导入的excel表中 （poi--自己制作）
        //2.1 创建工作薄
        /*Workbook wb=new XSSFWorkbook();*/
        Workbook wb = new SXSSFWorkbook();
        //2.2 创建工作页
        Sheet st = wb.createSheet();
        //2.3 指定列宽
        st.setColumnWidth(1,26*256);
        st.setColumnWidth(2,12*256);
        st.setColumnWidth(3,29*256);
        st.setColumnWidth(4,12*256);
        st.setColumnWidth(5,15*256);
        st.setColumnWidth(6,10*256);
        st.setColumnWidth(7,10*256);
        st.setColumnWidth(8,10*256);

        //2.4 设定基本参数
        int rowIndex = 0;
        Row row = null;
        Cell cell = null;
        //2.5 设定第一行
        row = st.createRow(rowIndex++); //先用0 再加1
        row.setHeightInPoints(36);//行高
        cell = row.createCell(1); //第一行的第一个单元格创建
        st.addMergedRegion(new CellRangeAddress(0,0,1,8));//合并单元格
        String value = inputDate.replaceAll("-0","-").replaceAll("-","年");  //2015-01  2015-11
        cell.setCellValue(value+"月份出货表"); //单元格内容
        cell.setCellStyle(bigTitle(wb));//单元格的样式
        //2.6 设定第二行
        row = st.createRow(rowIndex++); //先用1 再加1
        row.setHeightInPoints(26); //行高
        String[] arr = {"", "客户", "订单号", "货号", "数量", "工厂", "工厂交期", "船期", "贸易条款"};
        for(int i = 1; i < arr.length; i++){
            cell = row.createCell(i); //创建单元格
            cell.setCellValue(arr[i]); //单元格内容
            cell.setCellStyle(title(wb));// 设置单元格样式
        }
        //2.7 设定剩下的所有行
        for (ContractProductVo vo : list) {
            row = st.createRow(rowIndex++); //创建行 有一个contractProductVo创建一行
            row.setHeightInPoints(24); //设置每一行的行高

            cell = row.createCell(1);       //创建每一行的单元格
            cell.setCellValue(vo.getCustomName()); //设置单元格内容
            //cell.setCellStyle(text(wb)); //设置每行单元格的样式

            cell = row.createCell(2);       //创建每一行的单元格
            cell.setCellValue(vo.getContractNo()); //设置单元格内容
            //cell.setCellStyle(text(wb)); //设置每行单元格的样式

            cell = row.createCell(3);       //创建每一行的单元格
            cell.setCellValue(vo.getProductNo()); //设置单元格内容
            //cell.setCellStyle(text(wb)); //设置每行单元格的样式

            cell = row.createCell(4);       //创建每一行的单元格
            cell.setCellValue(vo.getCnumber()); //设置单元格内容
            //cell.setCellStyle(text(wb)); //设置每行单元格的样式

            cell = row.createCell(5);       //创建每一行的单元格
            cell.setCellValue(vo.getFactoryName()); //设置单元格内容
            //cell.setCellStyle(text(wb)); //设置每行单元格的样式

            cell = row.createCell(6);       //创建每一行的单元格
            cell.setCellValue(vo.getDeliveryPeriod()); //设置单元格内容
            //cell.setCellStyle(text(wb)); //设置每行单元格的样式

            cell = row.createCell(7);       //创建每一行的单元格
            cell.setCellValue(vo.getShipTime()); //设置单元格内容
            //cell.setCellStyle(text(wb)); //设置每行单元格的样式

            cell = row.createCell(8);       //创建每一行的单元格
            cell.setCellValue(vo.getTradeTerms()); //设置单元格内容
            //cell.setCellStyle(text(wb)); //设置每行单元格的样式
        }
        //3 提供下载功能
        //byteArrayOutputStream 字节流
        // response对象
        // 生成的文件名称
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        wb.write(os);//内容给流
        new DownloadUtil().download(os,response, value+ "月出货表.xlsx");


    }


    //大标题的样式
    public CellStyle bigTitle(Workbook wb){
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("宋体");
        font.setFontHeightInPoints((short)16);
        font.setBold(true);//字体加粗
        style.setFont(font);
        style.setAlignment(HorizontalAlignment.CENTER);				//横向居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);		//纵向居中
        return style;
    }

    //小标题的样式
    public CellStyle title(Workbook wb){
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("黑体");
        font.setFontHeightInPoints((short)12);
        style.setFont(font);
        style.setAlignment(HorizontalAlignment.CENTER);				//横向居中
        style.setVerticalAlignment(VerticalAlignment.CENTER);		//纵向居中
        style.setBorderTop(BorderStyle.THIN);						//上细线
        style.setBorderBottom(BorderStyle.THIN);					//下细线
        style.setBorderLeft(BorderStyle.THIN);						//左细线
        style.setBorderRight(BorderStyle.THIN);						//右细线
        return style;
    }

    //文字样式
    public CellStyle text(Workbook wb){
        CellStyle style = wb.createCellStyle();
        Font font = wb.createFont();
        font.setFontName("Times New Roman");
        font.setFontHeightInPoints((short)10);

        style.setFont(font);

        style.setAlignment(HorizontalAlignment.LEFT);				//横向居左
        style.setVerticalAlignment(VerticalAlignment.CENTER);		//纵向居中
        style.setBorderTop(BorderStyle.THIN);						//上细线
        style.setBorderBottom(BorderStyle.THIN);					//下细线
        style.setBorderLeft(BorderStyle.THIN);						//左细线
        style.setBorderRight(BorderStyle.THIN);						//右细线

        return style;
    }


    /*模板方式*/
    @RequestMapping(value = "/printExcel2",name = "导出出货表")
    public void printExcel(String inputDate) throws IOException {
        // 内容数据
        List<ContractProductVo> list = contractService.findOutData(companyId,inputDate);
        // 创建工作薄（指定excel模板）
        ServletContext servletContext = session.getServletContext();
        String path = servletContext.getRealPath("/make/xlsprint/tOUTPRODUCT.xlsx");
        Workbook wb = new XSSFWorkbook(path);
        Sheet st = wb.getSheetAt(0);
        // 设置基本参数
        int rowIndex = 0;
        Row row = null;
        Cell cell = null;

        // 获取第一行
        row = st.getRow(rowIndex++);
        cell = row.getCell(1);
        //替换内容
        String value = inputDate.replaceAll("-0","-").replaceAll("-","年");
        cell.setCellValue(value+"月份出货表");

        //跳过第二行
        rowIndex++;

        // 设定一个样式数组
        CellStyle[] cellStyle = new CellStyle[9];
        //内容行
        row = st.getRow(rowIndex);
        //获取第三行的每一个单元格
        for(int i = 1; i < row.getLastCellNum(); i++){
            cell = row.getCell(i); //获取每一个单元格
            cellStyle[i] = cell.getCellStyle(); //获取每一个单元格的样式
        }
        //设置内容 163
        for (ContractProductVo vo : list) {
            // 创建行
            row = st.createRow(rowIndex++);

            cell = row.createCell(1);
            cell.setCellValue(vo.getCustomName());
            cell.setCellStyle(cellStyle[1]);

            cell = row.createCell(2);
            cell.setCellValue(vo.getContractNo());
            cell.setCellStyle(cellStyle[2]);

            cell = row.createCell(3);
            cell.setCellValue(vo.getProductNo());
            cell.setCellStyle(cellStyle[3]);

            cell = row.createCell(4);
            cell.setCellValue(vo.getCnumber());
            cell.setCellStyle(cellStyle[4]);

            cell = row.createCell(5);
            cell.setCellValue(vo.getFactoryName());
            cell.setCellStyle(cellStyle[5]);

            cell = row.createCell(6);
            cell.setCellValue(vo.getDeliveryPeriod());
            cell.setCellStyle(cellStyle[6]);

            cell = row.createCell(7);
            cell.setCellValue(vo.getShipTime());
            cell.setCellStyle(cellStyle[7]);

            cell = row.createCell(8);
            cell.setCellValue(vo.getTradeTerms());
            cell.setCellStyle(cellStyle[8]);

        }

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        wb.write(os);//内容给流
        new DownloadUtil().download(os,response,"出货表.xlsx");
    }

}
